package com.cleaningcloud.auth.service;

import com.cleaningcloud.cld.api.domain.CldStaff;
import com.cleaningcloud.cld.api.model.LoginValidateInfo;
import com.cleaningcloud.cld.api.model.RegisterStaff;
import com.cleaningcloud.common.core.constant.SecurityConstants;
import com.cleaningcloud.common.core.domain.R;
import com.cleaningcloud.common.core.exception.ServiceException;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.redis.service.RedisService;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.api.RemoteCustomerService;
import com.cleaningcloud.erp.api.domain.ErpCustomer;
import com.cleaningcloud.erp.api.model.LoginCustomer;
import com.cleaningcloud.erp.api.model.RegisterCustomer;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * packageName com.cleaningcloud.auth.service
 *
 * @author 你的名字
 * @version JDK 8
 * @className MallLoginService
 * @date 2025-04-01
 * @description 三才环科商城登录服务类
 */
@Component
public class MallLoginService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private MiniPasswordService passwordService;

    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * @Author Shaw
     * @Description 三才环科商城(小程序)用户登录
     * @date: 2025-03-31 16:40
     * @param: //TODO
     */
    public LoginCustomer login(String userName, String password)
    {
        // 手机号或密码为空 错误
        if (StringUtils.isAnyBlank(userName, password))
        {
            throw new ServiceException("手机号/密码必须填写");
        }
        // 查询客户信息
        R<LoginCustomer> result = remoteCustomerService.getCustomerInfo(userName, SecurityConstants.INNER);
        if (R.FAIL == result.getCode())
        {
            throw new ServiceException(result.getMsg());
        }

        LoginCustomer customerInfo = result.getData();
        LoginValidateInfo loginValidateInfo = getCustomerLoginValidateInfo(result, customerInfo);
        passwordService.validate(loginValidateInfo, password);
        return customerInfo;
    }
    @NotNull
    private LoginValidateInfo getCustomerLoginValidateInfo(R<LoginCustomer> result, LoginCustomer customer) {
        LoginValidateInfo loginValidateInfo = new LoginValidateInfo();
        if(0 == customer.getApproveStatus()){
            throw new ServiceException("该账号管理员还在审核中,请稍后再试！");
        }else if(2 == customer.getApproveStatus()){
            throw new ServiceException("该账号已被管理员驳回,请联系管理员解除限制后再登录！");
        }else{
            loginValidateInfo.setPhone(customer.getUserName());
            loginValidateInfo.setPassword(customer.getPassWord());
        }
        return loginValidateInfo;
    }

    /**
     * @Author Shaw
     * @Description 三才环科商场(小程序)用户注册
     * @date: 2025-03-31 16:40
     * @param: //TODO
     */
    public void register(RegisterCustomer registerCustomer)
    {
        // 校验验证码是否正确
//        String code = redisService.getCacheObject(registerCustomer.getCode());
//        if(code == null){
//            throw new ServiceException("验证码已失效！");
//        }
//        if(StringUtils.isEmpty(registerCustomer.getCode())){
//            throw new ServiceException("验证码不能为空！");
//        }
//        if(!code.equals(registerCustomer.getCode())){
//            throw new ServiceException("传入的验证码错误！");
//        }
        String userName = redisService.getCacheObject(registerCustomer.getUserName());
        if("".equals(userName) && userName == null){
            throw new ServiceException("用户登录名不能为空！");
        }
        ErpCustomer erpCustomer = new ErpCustomer();
        erpCustomer.setPassWord(SecurityUtils.encryptPassword(registerCustomer.getPassword()));
        erpCustomer.setUserName(registerCustomer.getUserName());
        erpCustomer.setCustomerName(registerCustomer.getCustomerName());
        erpCustomer.setCompanyName(registerCustomer.getCompanyName());
        R<?> registerResult = remoteCustomerService.registerCustomerInfo(erpCustomer, SecurityConstants.INNER);

        if (R.FAIL == registerResult.getCode())
        {
            throw new ServiceException(registerResult.getMsg());
        }
    }
}